package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.entity.students;
import com.example.springboot.service.SignupFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = "http://localhost:7070")
@RestController
@RequestMapping("/SignupForm")
public class SignupFormController {
    @Autowired
    private SignupFormService signupFormService;

    @GetMapping("/students")
    public Result getAllStudents() {
        try {
            List<students> students = signupFormService.findAllstudents();
            return Result.success(students);
        } catch (Exception e) {
            e.printStackTrace(); // 打印异常堆栈信息，便于调试
            return Result.error("500", "获取学生列表失败");
        }
    }

    @PostMapping("/students")
    public Result createStudent(@RequestBody students students) {
        try {
            signupFormService.savestudents(students);
            return Result.success("报名成功！");
        } catch (Exception e) {
            e.printStackTrace(); // 打印异常堆栈信息，便于调试
            return Result.error("500", "报名失败，请重试。");
        }
    }

    @GetMapping("/checkStudentId")
    public Result checkStudentId(@RequestParam int student_id) {
        try {
            boolean exists = signupFormService.checkStudentId(student_id);
            if (exists) {
                return Result.success("学生ID存在，可以报名。");
            } else {
                return Result.error("404", "学生ID不存在，请重新输入。");
            }
        } catch (Exception e) {
            e.printStackTrace(); // 打印异常堆栈信息，便于调试
            return Result.error("500", "检查学生ID失败，请重试。");
        }
    }
}